package com.cyrus.rabbitm.listener;

import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.listener.api.ChannelAwareMessageListener;
import org.springframework.stereotype.Component;

/**
 * Consumer限流机制
 * 1、确保ack机制为手动确认 listener:simple:acknowledge-mode: manual
 * 2、listener-container 配置属性
 *      1、perfetch=1：表示消费端每次从mq中拉去一条消息，直到手动签收消费完毕后，再去拉去下一条消息
 *
 */
@Component
public class QosListener implements ChannelAwareMessageListener {
    //@RabbitListener(queues = "boot_queue_ttl")
    @Override
    public void onMessage(Message message, Channel channel) throws Exception {
        Thread.sleep(2000);
            //1、接收消息（默认拉去所有消息）
            System.out.println(new String(message.getBody()));

            //2、业务处理

            //3、签收消息
           channel.basicAck(message.getMessageProperties().getDeliveryTag(), true);
    }
}
